//
//  ShipperContractListVC.m
//  NtoccShipper
//
//  Created by mac on 2020/6/22.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "ShipperContractListVC.h"
#import "ShipperContractListModel.h"
#import "ShipperContractListCell.h"
#import "CJWKWebViewVC.h"
#import "XLImageViewer.h"

@interface ShipperContractListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ShipperContractListCell *shipperContractListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) CJLabel *labSerialNum;
@property (nonatomic,strong) CJLabel *labSignTime;
@end

@implementation ShipperContractListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"货主合同")];
    [self setBackLeftButton:@""];
    
    self.shipperContractListCell = [[ShipperContractListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/shipmentContract",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":@(self.page),
                             @"pageSize":@"10",
//                                 @"isCancel":self.strIsCancel
                            };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
        
        if ([ws.refreshType isEqualToString:@"down"]) {
            [ws.modelArray removeAllObjects];
        }

        if (list.count > 0) {
            
            for (int i = 0; i < list.count; i++) {
                NSDictionary *dic = list[i];
                NSError * error ;
                ShipperContractListModel *shipperContractListModel = [[ShipperContractListModel alloc]initWithDictionary:dic error:&error];
                NSArray *listAdress = shipperContractListModel.fileMap;
                NSMutableArray *arrAdress = [[NSMutableArray alloc]init];

                if (listAdress.count > 0) {

                    for (int y = 0; y < listAdress.count; y++ ) {
                         NSDictionary *dicAdress = listAdress[y];
                         FileMapModel *addressModel = [[FileMapModel alloc]initWithDictionary:dicAdress error:&error];
                         [arrAdress addObject:addressModel];
                     }

                }else{
                    //FileMapModel *addressModel = [[FileMapModel alloc]initWithDictionary:nil error:&error];
                    FileMapModel *addressModel = [[FileMapModel alloc]init];
                    addressModel.name = @"";
                    [arrAdress addObject:addressModel];
                }
                
                shipperContractListModel.fileMap = arrAdress;
                [self.modelArray addObject:shipperContractListModel];
            }

            [ws.tableView reloadData];
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }

        }else{
            if ([total isEqualToString:@"0"]) {
                [ws.modelArray removeAllObjects];
            }
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh
{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.modelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ShipperContractListModel *sourceListModel = self.modelArray[section];
    return sourceListModel.fileMap.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"ShipperContractListCell";
    ShipperContractListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ShipperContractListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    ShipperContractListModel *sourceListModel = self.modelArray[indexPath.section];
    FileMapModel *model = sourceListModel.fileMap[indexPath.row];
    [cell setCellInfoWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShipperContractListModel *sourceListModel = self.modelArray[indexPath.section];
    FileMapModel *model = sourceListModel.fileMap[indexPath.row];
    NSArray *arrList = [model.name componentsSeparatedByString:@"."];
    
    if (arrList.count >= 2) {
        // docx   jpg   pdf
        if ([[CJControl getThreeUnaryValueStr:arrList[1]] isEqualToString:@"pdf"]) {
            
            CJWKWebViewVC *vc = [[CJWKWebViewVC alloc]init];
            vc.strTitle = model.name;
            vc.strUrl = model.url;
            [self pushVc:vc];
        }else  if ([[CJControl getThreeUnaryValueStr:arrList[1]] isEqualToString:@"docx"]) {
                   
           CJWKWebViewVC *vc = [[CJWKWebViewVC alloc]init];
           vc.strTitle = model.name;
           vc.strUrl = model.url;
           [self pushVc:vc];
        }else{
            // 获取当前点击的cell
            ShipperContractListCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            [[XLImageViewer shareInstanse] showNetImages:@[model.url] index:0 fromImageContainer:cell];
        }
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  [self viewForHeaderInSection:section].height;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  [self viewForHeaderInSection:section];
}

- (FlexFrameView *)viewForHeaderInSection:(NSInteger)section{

    ShipperContractListModel *model = self.modelArray[section];
    FlexFrameView* headerView = [[FlexFrameView alloc] initWithFlex:@"ShipperContractListSectionView" Frame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0) Owner:self];
    headerView.flexibleHeight = YES;

    self.labSerialNum.text = model.serialNum;
    self.labSignTime.text = model.signTime;

    [headerView layoutIfNeeded];
    return headerView;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight + kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight -CJTableToTopHeight ),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    [_tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    return _tableView;
}

@end
